﻿using SuporeSliplampApp.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// CameraSettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CameraSettingWindow : Window
    {
        public CameraSettingWindow()
        {
            InitializeComponent();
            initData();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void expoSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.EXPOTIME, (int)expoSlider.Value);
                r_txt.Text = ((int)expoSlider.Value).ToString();
            }
        }

        private void r_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.WBRED, (int)r_slider.Value);
                r_txt.Text = ((int)r_slider.Value).ToString();
            }
        }

        private void g_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.WBGREEN, (int)g_slider.Value);
                g_txt.Text = ((int)g_slider.Value).ToString();
            }
        }

        private void b_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((CameraView.cam_ != null) && CommonData.started_)
            {
                CameraView.cam_.put(uvcham.eCMD.WBBLUE, (int)b_slider.Value);
                b_txt.Text = ((int)b_slider.Value).ToString();
            }
        }

        private void initData()
        {
            auto_exposure.IsEnabled = CommonData.Expo_enabled;
            white_balance.IsEnabled = CommonData.balance_enabled;
            expoSlider.IsEnabled = CommonData.Expo_enabled;
            r_slider.IsEnabled = CommonData.R_enabled;
            g_slider.IsEnabled = CommonData.G_enabled;
            b_slider.IsEnabled = CommonData.B_enabled;
            r_slider.IsEnabled = true;
            g_slider.IsEnabled = true;
            b_slider.IsEnabled = true;
            auto_exposure.IsChecked = CommonData.Expo_checked;
            white_balance.IsChecked = CommonData.balance_checked;
            expoSlider.Maximum = CommonData.ExpoSlider_Max;
            expoSlider.Minimum = CommonData.ExpoSlider_Min;
            r_slider.Maximum = CommonData.R_Max;
            r_slider.Minimum = CommonData.R_Min;
            g_slider.Maximum = CommonData.R_Max;
            g_slider.Minimum = CommonData.R_Min;
            b_slider.Maximum = CommonData.B_Max;
            b_slider.Minimum = CommonData.B_Min;

            if (Common.isexpo == true)
            { 
                auto_exposure.IsChecked = true;
                expoSlider.IsEnabled = false;
            }
            if (Common.isexpo == false)
            { 
                auto_exposure.IsChecked = false;
                expoSlider.IsEnabled = true;
            }

            if(Common.iswhite == true) 
            {
                white_balance.IsChecked=true;
            }
            if (Common.iswhite == false) 
            {
                white_balance.IsChecked=false;
            }

        }

        private void white_balance_Click(object sender, RoutedEventArgs e)
        {
            int temp = 0;
            if (CommonData.started_)
            {
                //CameraView.cam_?.put(uvcham.eCMD.WBMODE, (white_balance.IsChecked ?? false) ? 1 : 0);
                if (white_balance.IsChecked == false)
                {
                    temp = 0;
                    r_slider.IsEnabled = true;
                    g_slider.IsEnabled = true;
                    b_slider.IsEnabled = true;

                    Common.iswhite = false;
                }
                else
                {
                    temp = 1;
                    r_slider.IsEnabled = false;
                    g_slider.IsEnabled = false;
                    b_slider.IsEnabled = false;

                    Common.iswhite = true;
                }
                CameraView.cam_?.put(uvcham.eCMD.WBMODE, temp);

                //r_slider.IsEnabled = !(white_balance.IsChecked ?? false);
                //g_slider.IsEnabled = !(white_balance.IsChecked ?? false);
                //b_slider.IsEnabled = !(white_balance.IsChecked ?? false);
            }

        }

        private void auto_exposure_Click(object sender, RoutedEventArgs e)
        {
            if (CommonData.started_)
            {
                CameraView.cam_?.put(uvcham.eCMD.AEXPO, (auto_exposure.IsChecked ?? false) ? 1 : 0);
                expoSlider.IsEnabled = !(auto_exposure.IsChecked ?? false);
                if (auto_exposure.IsChecked == true)
                {
                    Common.isexpo = true;
                }
                else 
                { 
                    Common.isexpo = false;
                }

            }
        }


        private void BtnSwitch_Click(object sender, RoutedEventArgs e)
        {
            if (CommonData.started_)
            {
                int ret = uvcham.switcFlg;
                CameraView.cam_?.put(uvcham.eCMD.USER, (uvcham.switcFlg == 1) ? 0 : 1);
                uvcham.switcFlg = (uvcham.switcFlg == 1 ? 0 : 1);

            }
        }

        private void LightSetting_Click(object sender, RoutedEventArgs e)
        {
            LightControlView lightControlView = new LightControlView();
            lightControlView.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            lightControlView.ShowDialog();
        }


        private void Colorset_Click(object sender, RoutedEventArgs e)
        {
            CameraView.cam_?.put(uvcham.eCMD.WBRED, 50);
            CameraView.cam_?.put(uvcham.eCMD.WBGREEN, 68);
            CameraView.cam_?.put(uvcham.eCMD.WBBLUE, 328);
        }



    }
}
